//package com.hzwq.demo2.filter;
//
//import com.hzwq.demo2.permission.entity.Permission;
//import com.hzwq.demo2.role.entity.Role;
//import com.hzwq.demo2.role.service.IRoleService;
//import com.hzwq.demo2.rolepermission.entity.RolePermission;
//import com.hzwq.demo2.user.entity.User;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//import java.util.*;
//
////@Component  注意这个component
//@WebFilter(urlPatterns = {"/user/*","/role/*","/permission/*"})
//public class LoginFilter implements Filter {
//
//    @Autowired
//    IRoleService roleService;
//
//    @Override
//    public void destroy() {
//    }
//
//    @Override
//    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//        /**
//         * 1.判断用户是否登录
//         * 2.登录，继续
//         * 3.未登录，跳转到login.jsp页面
//         */
//
//        HttpServletRequest request = (HttpServletRequest) req;
//        HttpServletResponse response=(HttpServletResponse) resp;
//        response.setContentType("text/html;charset=utf-8");
//        HttpSession session = request.getSession();
//        String requestURI = request.getRequestURI();
//        System.out.println("===="+requestURI);
//        User user = (User) session.getAttribute("user");
//        if (user == null) {
//            response.sendRedirect("/demo2/login");
////            System.out.println("hello world!");
////            return;
//        } else {
//            //判断访问的是那个url
//            String[] split = requestURI.split("/");
//            List<Role> roleList = user.getRoleList();
//           // System.out.println(roleList);
//            ArrayList<String> nameList = new ArrayList<>();
//            //知道这个user都有哪些角色
//            roleList.forEach(n->nameList.add(n.getRoleName()));
//            //取出每个角色所对应的权限
//            List<Permission> allPermissionList=new ArrayList<>();
//            //List<RolePermission> rolePermissionList = null;
//            List<Permission> permissionList=new ArrayList<>();
//            for (int i = 0; i < roleList.size(); i++) {
//               // rolePermissionList = roleList.get(i).getRolePermissionList();
//                //根据role.id查询rolepermission表来获取url
//                permissionList  = roleService.selectUrl(roleList.get(i).getId());
//                if (permissionList!=null){
//                    allPermissionList.addAll(permissionList);
//                }
//              //  allRolePermissionList.addAll(rolePermissionList);
//            }
//            //得到所有的权限id,但是里面有重复的
////            System.out.println(allPermissionList);
//            LinkedHashMap linkedHashMap = new LinkedHashMap();
//            //存下所有的权限访问路径
//            for (Permission p : allPermissionList) {
//
//                linkedHashMap.put(p.getId(),p.getPage());
//            }
//            linkedHashMap.put("/demo2/user/index","/demo2/user/index");
//            if (linkedHashMap.containsValue(requestURI)){
//                chain.doFilter(request, response);
//            }else{
//                response.getWriter().write("<script>alert('您没有该权限！'); window.location='../NoPer'; window.close();</script>");
//                response.getWriter().flush();
//            }
//////            System.err.println(linkedHashMap);
////            //进行权限判断
////            if(nameList.contains("超级管理员")){
////                chain.doFilter(request, response);
////            }else{
////                if (split[split.length-2].equals("user")||split[split.length-2].equals("role")){
////                    chain.doFilter(request, response);
////                }else{
////                    response.getWriter().write("<script>alert('您没有该权限！'); window.location='../NoPer'; window.close();</script>");
////                    response.getWriter().flush();
////                }
////            }
////            //////////////////
//////            chain.doFilter(req, resp);
//////            System.out.println("...");
//        }
//    }
//
//    @Override
//    public void init(FilterConfig config) throws ServletException {
//
//    }
//
//}
